package com.gtlab1207.br_security.ctrls;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.gtlab1207.br_security.common.Codes;
import com.gtlab1207.br_security.common.Result;
import com.gtlab1207.br_security.models.domain.User;
import com.gtlab1207.br_security.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user/")
public class UserCtrl extends BaseCtrl{
    @Autowired
    private UserService userService;

    @RequestMapping("doLogin")
    public Result doLogin(String username, String password) {
        try {
            User user = userService.selectUser(username);
            if (user != null && password.equals(user.getPassword())) {
                StpUtil.login(username);
                SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
                return this.send(tokenInfo, 1, "登录成功");
            }
            return this.send(Codes.LOGIN_FAILED);
        }catch (Exception e){
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    @RequestMapping("username")
    public Result getUsername() {
        if(StpUtil.isLogin()){
            String loginId=StpUtil.getLoginIdAsString();
            String username=loginId.substring(0,loginId.indexOf("_"));
            Map<String, String> result = new HashMap<String, String>();
            result.put("username", username);
            return this.send(result);
        }
        return this.send(Codes.NO_LOGIN);
    }

    @RequestMapping("isLogin")
    public Result isLogin() {
        Map<String, Boolean> result = new HashMap<String, Boolean>();
        result.put("isLogin", StpUtil.isLogin());
        return this.send(result);
    }

    @RequestMapping("logout")
    public Result logout() {
        StpUtil.logout();
        return  this.send(Codes.SUCCESS);
    }

    @PostMapping("info")
    public Result insertUser(@Valid User user){
        try {
            Integer result = userService.insertUser(user);
            if(result==0){
                return this.send(1101,"新增失败，请重新录入信息");
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    @PutMapping("info")
    public Result updateUser(User user){
        try {
            Integer result = userService.updateUser(user);
            if(result==0){
                return this.send(Codes.MOD_FAILED);
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

    @DeleteMapping("info/{username}")
    public Result updateUser(@PathVariable String username){
        try {
            Integer result = userService.deleteUser(username);
            if(result==0){
                return this.send(Codes.FAILED);
            }
            return this.send(Codes.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return this.send(Codes.FAILED);
        }
    }

}
